import 'package:dio/dio.dart';
import 'package:thinking/config/_.dart';
import 'package:thinking/entity/_.dart';
import 'package:thinking/http/Dio.dart';
import 'ClassService.dart';
class HomeService{

  static Future<ClassResult?> getClassData({Map<String,dynamic>? params }) async {

    List<Map<String,dynamic>> a = await ClassService.findAll("Principal");
    if (a.isNotEmpty) {
      List<Map<String, dynamic>> b = await ClassService.findAll("Additional");
      return ClassResult.fromJson({
        'principal': a,
        'additional': b,
      });
    }else{
      var res = await HttpApi().get(
          url: Config.apiUrl['ApiList']['getClass'],
          params: params,
          options:Options(extra:{'showLoading':true})
      );
      var resJson = ClassEntity.fromJson(res).data;
      await ClassService.addList("Principal",resJson!.principal);
      await ClassService.addList("Additional",resJson.additional);
      return resJson;
    }

  }



  ///查询推荐页数据
  static Future<HomeResult?> getHomeData({Map<String,dynamic>? params }) async{
    var res = await HttpApi().get(
        url: Config.apiUrl['ApiList']['getHomeData'],
        params: params,
        options:Options(extra:{'showLoading':params!['isShowHttpLoading']})
    );
    return HomeEntity.fromJson(res).data;

  }

  ///点赞||取消点赞think
  static Future<dynamic> isLikeThink({Map<String,dynamic>? params }) async{
    var res = await HttpApi().get(
        url: Config.apiUrl['ApiList']['isLikeThink'],
        params: params,
        options:Options(extra:{'showLoading':true})
    );

    return res['data'];

  }
  ///收藏||取消收藏think
  static Future<dynamic> isCollectionThink({Map<String,dynamic>? params }) async{
    var res = await HttpApi().get(
        url: Config.apiUrl['ApiList']['isCollectionThink'],
        params: params,
        options:Options(extra:{'showLoading':true})
    );

    return res['data'];

  }

  ///think详情
  static Future<Think> getThinkInfo({Map<String,dynamic>? params }) async{
    var res = await HttpApi().get(
        url: Config.apiUrl['ApiList']['getThinkInfo'],
        params: params,
        options:Options(extra:{'showLoading':false})
    );

    return Think.fromJson(res['data']);

  }

  ///浏览记录
  static Future<dynamic> setRecentVisit({Map<String,dynamic>? params }) async{
    return await HttpApi().get(
        url: Config.apiUrl['ApiList']['setRecentVisit'],
        params: params,
        options:Options(extra:{'showLoading':false})
    );



  }

}